<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="shortcut icon" href="C:\Users\zzz\Desktop\Reac\favicon.ico" />
    <title>Document</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="../../js/react.development.js"></script>
    <script src="../../js/react-dom.development.js"></script>
    <script src="../../js/prop-types.js"></script> 
    <script src="../../js/babel.min.js"></script>
    <script type="text/babel">
        /*
        高级函数：
        接受的参数为函数
        a为函数返回值依然是函数
        */
    class Login extends React.Component {
        state = {
            username: '',
            password: ''
        }
        // saveUsername = (event)=> {
        //     this.setState({username:event.target.value})
        // }
        // savePassword= (event)=> {
        //     this.setState({password:event.target.value})
        // }
         saveFromDara = (dataType)=> {
            return ()=> {
                this.setState({[dataType]: event.target.value})            
            }
        }
        handleSubmit = (e)=> {
            // 阻止表单提交
            e.preventDefault();
            const {username , password} = this.state
            alert(`Welcome ${username} ${password}`)
        }
        render() {
            return(
                <form  onSubmit = {this.handleSubmit}>
                {/*onchange改变就调用*/}
                User: <input type="text" onChange = {this.saveFromDara('username')} name = "uername"/>
                password: <input type="password" onChange = {this.saveFromDara('password')} password = "password"/> 
                <button>登录</button>       
                </form>
            )
        }
    }

    ReactDOM.render(<Login/>,document.getElementById('root'))

    
    </script>
  </body>
</html>
